package SocketBaseTest;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by yexuan on 2018/3/24.
 * 简历文本转换器
 * 客户端想服务端发送文本，服务端将其转换为大写返回给客户端。
 */
public class TcpDemo {
}

class TransClient{
    public static void main(String[] args) throws Exception {
        Socket s = new Socket("172.16.35.23",10002);
        //读取键盘
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //将数据发给服务端
        BufferedWriter bwOut = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        //获取服务器返回的大写信息
        BufferedReader brIn = new BufferedReader(new InputStreamReader(s.getInputStream()));

        String line = null;
        while ((line = br.readLine())!=null){
            if("over".equals(line))
                break;
            bwOut.write(line);
            bwOut.newLine();  //结束标记  否则readLine方法会一直等待
            bwOut.flush();  //刷新缓冲区中的数据

            String str = brIn.readLine();
            System.out.println("server translate..."+str);
        }
        br.close();
        s.close();
    }
}

class TransServer{
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(10002);
        Socket s = ss.accept();

        String ip = s.getInetAddress().getHostAddress();
        System.out.println(ip + "connect.....");
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

        String line = null;
        while ((line = br.readLine())!=null){
            String str = line.toUpperCase();
            bw.write(str);
            bw.newLine();  //结束标记
            bw.flush();  //刷新缓冲区中的数据
        }
        s.close();
        ss.close();

    }

}
